package com.emellend.api.v4;

import com.emellend.api.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class FragmentAlertDialogSupport extends FragmentActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_dialog);
		init();
	}

	private void init() {
		TextView tv = (TextView) this.findViewById(R.id.text);
		tv.setText("Example of displaying an alert dialog with a DialogFragment");
		this.findViewById(R.id.show).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog();
			}
		});
	}
	
	private void showDialog() {
		DialogFragment newFragment = MyAlertDialogFragment.newInstance(R.string.title);
		newFragment.show(getSupportFragmentManager(), "dialog");
	}
	
	public void doPositiveClick() {
        Log.i("FragmentAlertDialog", "Positive click!");
    }

    public void doNegativeClick() {
        Log.i("FragmentAlertDialog", "Negative click!");
    }
    
	public static class MyAlertDialogFragment extends DialogFragment{
		
		private MyAlertDialogFragment(){}
		
		public static MyAlertDialogFragment newInstance(int title) {
			MyAlertDialogFragment frag=new MyAlertDialogFragment();
			Bundle args=new Bundle();
			args.putInt("title", title);
			frag.setArguments(args);
			return frag;
		}

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			int title=getArguments().getInt("title");
			
			return new AlertDialog.Builder(getActivity())
					.setIcon(R.drawable.alert_dialog_icon)
					.setTitle(title)
					.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							((FragmentAlertDialogSupport)getActivity()).doPositiveClick();
						}
					})
					.setNegativeButton("", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							((FragmentAlertDialogSupport)getActivity()).doNegativeClick();
						}
					})
					.create();
		}
		
	}
}
